package com.cslc.wechat.oauth.action;

import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.cslc.wechat.common.action.BaseAction;
import com.cslc.wechat.common.bean.Terminal;
import com.cslc.wechat.common.dto.JsApiTicketInfo;
import com.cslc.wechat.common.mp.MpInfo;
import com.cslc.wechat.common.service.WechatService;
import com.cslc.wechat.oauth.service.OauthService;
import com.cslc.wechat.util.JsSignUtil;
import com.cslc.wechat.util.WeChatUtil;

public class OauthAction extends BaseAction{
	private static Logger logger = Logger.getLogger(OauthAction.class);
	private WechatService wechatService;
	private OauthService oauthService;
	private String timestamp;
	private String nonceStr;
	private String signature;
	private String openid;
	private String nickname;
	private Set<Terminal> terminals;

	public WechatService getWechatService() {
		return wechatService;
	}

	public void setWechatService(WechatService wechatService) {
		this.wechatService = wechatService;
	}

	public OauthService getOauthService() {
		return oauthService;
	}

	public void setOauthService(OauthService oauthService) {
		this.oauthService = oauthService;
	}

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public String getNonceStr() {
		return nonceStr;
	}

	public void setNonceStr(String nonceStr) {
		this.nonceStr = nonceStr;
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public String execute() throws Exception {
		HttpServletRequest request = this.request();
		HttpServletResponse response = this.response();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		//用户同意授权后获取code
		String code = request.getParameter("code");
		logger.info("code: " + code);
		//用户同意授权
		if(code != null && !"authdeny".equals(code)){
			JSONObject jsonObject = wechatService.getOauth2Acess(code);
			openid = jsonObject.getString("openid");
			logger.info("openid: " + openid);
			JSONObject jsonUser = wechatService.getUserInfo(openid);
			setNickname(jsonUser.getString("nickname"));
			terminals = oauthService.getClerkBindTerminals(openid);
			//已绑定过
			if(null != terminals && 0 < terminals.size()) {
				JsApiTicketInfo jsapi_ticket  = WeChatUtil.getJsApiTicket();
				String url = MpInfo.HOST + "/fault_report?code=" + code + "&state=STATE";
				Map<String, String> ret = JsSignUtil.sign(jsapi_ticket.getTicket(), url);
				timestamp = ret.get("timestamp");
				nonceStr = ret.get("nonceStr");
				signature = ret.get("signature");
				logger.info("signature:" + signature);
				return SUCCESS;
			}
			else {
				return "unbind";
			}
			
		}
		
		//授权失败
		else
			return ERROR;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public Set<Terminal> getTerminals() {
		return terminals;
	}

	public void setTerminals(Set<Terminal> terminals) {
		this.terminals = terminals;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
}
